interface IFormInfo {
  name: string
  pass: string
  rePassword?: string
}

export default function useValidator(ruleForm: IFormInfo) {
  //重复输入密码校验
  const validateRepass = (rule, value, callback) => {
    const trimValue = value.trim()
    if (!trimValue) {
      callback(new Error("请再次输入密码"))
    } else if (trimValue !== ruleForm.pass) {
      callback(new Error("两次输入密码不一致!"))
    } else {
      callback()
    }
  }
  // 用户名校验
  const validateName = (rule, value, callback) => {
    const trimValue = value.trim()
    if (!trimValue) {
      callback(new Error("请输入用户名"))
    } else if (!/^\w{6,13}$/.test(trimValue)) {
      callback(new Error("请输入数字、字母、下划线,且长度为6 到13 个字符"))
    } else {
      callback()
    }
  }
  // 密码校验
  const validatePass = (rule, value, callback) => {
    const trimValue = value.trim()
    if (!trimValue) {
      callback(new Error("请输入密码"))
    } else if (!/^\w{8,18}$/.test(trimValue)) {
      callback(new Error("请输入数字、字母、下划线,且长度为8 到18 个字符"))
    } else {
      callback()
    }
  }

  const rules = {
    name: [{ validator: validateName, trigger: "blur" }],
    pass: [{ validator: validatePass, trigger: "blur" }],
    rePassword: [{ validator: validateRepass, trigger: "blur" }]
  }

  return {
    rules
  }
}
